package com.bookRecommend.service.impl;


import com.bookRecommend.commons.MD5Utils;
import com.bookRecommend.mapper.IAdminMapper;
import com.bookRecommend.model.Admin;
import com.bookRecommend.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class AdminServiceImpl implements IAdminService {

    @Autowired
    IAdminMapper mapper;

    @Override
    public List<Admin> findAllAdmin() {
        return mapper.selectAllAdmin();
    }

    @Override
    public Admin findByAdmin(Admin admin) {
        return mapper.selectByUserNameAnaPassword(admin);
    }

    @Override
    public Admin findByUserNameANdPassword(String userName, String password) {
        if (userName == null || userName.isEmpty()) {
            return null;
        }
        Admin admin = new Admin();
        admin.setUsername(userName);
        admin.setPassword(MD5Utils.encode(password));
        return mapper.selectByUserNameAnaPassword(admin);
    }

    @Override
    public int addAdmin(String userName, String password) {
        if (userName == null || userName.length() == 0 || password == null || password.length() == 0) {
            return 0;
        }
        Admin admin = new Admin();
        admin.setUsername(userName);
        admin.setPassword(MD5Utils.encode(password));
        return mapper.insertAdmin(admin);
    }

    @Override
    public int modify(String password, Integer id) {
        password = MD5Utils.encode(password);
        return mapper.update(password, id);
    }


}
